package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.MyObject;

/**
 * @author Hnmtuan
 * @version 1.0
 * @created 01-Aug-2011 3:15:11 PM
 */
public class Files extends MyObject {


    public static List<pojo.Files> getFilesByNames(String fileNames) {
        List<pojo.Files> results = new ArrayList<pojo.Files>();
        Connection conn = null;
        try {

            MyObject.CreateConnectStatic();
            conn = MyObject.getConnectionStatic();

            String sql = "SELECT f.* FROM files f WHERE f.FName LIKE ? OR  f.FName LIKE ? OR  f.FName LIKE ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, fileNames+'%');
            ps.setString(2, '%'+fileNames);
            ps.setString(3,  '%'+fileNames+'%');

            ps.executeQuery();

            ResultSet rs = ps.getResultSet();

            while (rs.next()) {
                pojo.Files obj = new pojo.Files(rs.getString("idFile"), rs.getString("FName"), rs.getString("FDescription"),
                                                         rs.getString("Peer"), rs.getInt("FState"), rs.getDate("FCreatedDate"),
                                                         rs.getDate("FLastUpdated"), rs.getString("Peers_IdPer"));
                results.add(obj);
            }


        } catch (Exception e) {
            String messages = String.format("getFilesByNames : %s ", e.getMessage());
            System.out.println(messages);
        } finally {
            try {
                conn.close();
                MyObject.CloseConnectStatic();
            } catch (Exception e) {
                String messages = String.format("getFilesByNames : %s ", e.getMessage());
                System.out.println(messages);
            }
        }

        return results;
    }
}
